package com.chris.homecare.screens
{
	import com.chris.homecare.components.BreadcrumbComponenet;
	import com.chris.homecare.components.HeaderComponent;
	import com.chris.homecare.components.StarRatting;
	import com.chris.homecare.data.Employee;
	import com.chris.homecare.data.FetchRemoteData;
	import com.chris.homecare.utils.CommonUtils;
	import com.chris.homecare.utils.ScreenNames;
	
	import flash.filesystem.File;
	
	import feathers.controls.Button;
	import feathers.controls.ImageLoader;
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.layout.AnchorLayout;
	import feathers.layout.AnchorLayoutData;
	import feathers.layout.HorizontalLayout;
	import feathers.layout.VerticalLayout;
	
	import starling.display.Quad;
	import starling.events.Event;
	
	[Event(name="triggerManagersReview",type="starling.events.Event")]
	[Event(name="triggerCustomerReview",type="starling.events.Event")]
	[Event(name="triggerWriteReview",type="starling.events.Event")]
	
	public class PerformanceScreen extends LayoutGroup
	{
		public static const TRIGGER_MANAGERS_REVIEW:String = "triggerManagersReview";
		public static const TRIGGER_CUSTOMER_REVIEW:String = "triggerCustomerReview";
		public static const TRIGGER_WRITE_REVIEW:String = "triggerWriteReview";
		
		private var buttonsLayoutGroup:LayoutGroup;
		private var buttonsBackground:Quad;
		private var header:HeaderComponent;
		private var reviewBtn:Button;
		private var managersReviewBtn:Button;
		private var custReviewBtn:Button;
		private var breadcrumbComp:BreadcrumbComponenet;
		private var anchorLayoutGroup:LayoutGroup;
		private var employeeName:Label;
		private var empTitleLblData:AnchorLayoutData;
		private var employeeImage:ImageLoader;

		private var employeeTitle:Label;
		private var employeeImageData:AnchorLayoutData;
		private var imageFrame:ImageLoader;
		private var starRattingComp:StarRatting;
		private var starRatingCompData:AnchorLayoutData;

		private var vertBtnsLayoutGroup:LayoutGroup;
		private var employeeData:Employee;

		public function PerformanceScreen()
		{
			super();
		}
		
		override protected function initialize():void
		{
			CommonUtils.previousScreen = ScreenNames.EMPLOYEE_SCREEN;
			
			employeeData = FetchRemoteData.choosenEmployeeData;
			
			this.layout = new AnchorLayout();
			
			var topVerticalLayoutGroup:LayoutGroup = new LayoutGroup();
			topVerticalLayoutGroup.layout = new VerticalLayout();
			addChild(topVerticalLayoutGroup);
			
			header = new HeaderComponent();
			header.addEventListener(Event.COMPLETE, onMenuBtnTrigger);
			topVerticalLayoutGroup.addChild(header);
			
			breadcrumbComp = new BreadcrumbComponenet("Info", "Team");
			topVerticalLayoutGroup.addChild(breadcrumbComp);
			
			anchorLayoutGroup = new LayoutGroup();
			var anchorLayout:AnchorLayout = new AnchorLayout();
			anchorLayoutGroup.layout = anchorLayout;
			topVerticalLayoutGroup.addChild(anchorLayoutGroup);
			
			employeeName = new Label();
			employeeName.nameList.add("employee-name-label");
			employeeName.text = employeeData.name;
			var empNameLblData:AnchorLayoutData = new AnchorLayoutData();
			empNameLblData.horizontalCenter = 0;
			empNameLblData.top = 10;
			employeeName.layoutData = empNameLblData;
			anchorLayoutGroup.addChild(employeeName);
			
			employeeTitle = new Label();
			employeeTitle.nameList.add("employee-title-label");
			employeeTitle.text = employeeData.position;
			anchorLayoutGroup.addChild(employeeTitle);
			empTitleLblData = new AnchorLayoutData();
			empTitleLblData.horizontalCenter = 0;
			employeeTitle.layoutData = empTitleLblData;
			
			employeeImage = new ImageLoader();
			employeeImage.source = File.applicationStorageDirectory.resolvePath(employeeData.imageUrl).url;
			employeeImageData = new AnchorLayoutData();
			employeeImageData.horizontalCenter = 0;
			employeeImage.layoutData = employeeImageData;
			anchorLayoutGroup.addChild(employeeImage);
			
			imageFrame = new ImageLoader();
			imageFrame.source = "assets/images/homecare/image-overlay.png";
			imageFrame.layoutData = employeeImageData;
			anchorLayoutGroup.addChild(imageFrame);
			
			starRattingComp = new StarRatting(false);
			starRatingCompData = new AnchorLayoutData();
			starRatingCompData.horizontalCenter = 0;
			starRatingCompData.bottom = 0;
			starRattingComp.layoutData = starRatingCompData;
			anchorLayoutGroup.addChild(starRattingComp);
			starRattingComp.setSelectedForNum(int(employeeData.ratingNumber));
			
			buttonsLayoutGroup = new LayoutGroup();
			var anchorLayoutData:AnchorLayoutData = new AnchorLayoutData();
			anchorLayoutData.bottom = 0;
			buttonsLayoutGroup.layoutData = anchorLayoutData;
			addChild(buttonsLayoutGroup);
			
			buttonsBackground = new Quad(10,10,0x64a950);
			buttonsLayoutGroup.addChild(buttonsBackground);
			
			vertBtnsLayoutGroup = new LayoutGroup();
			var btnsLayout:VerticalLayout = new VerticalLayout();
			btnsLayout.horizontalAlign = HorizontalLayout.HORIZONTAL_ALIGN_CENTER;
			btnsLayout.paddingTop = btnsLayout.paddingBottom = 30;
			btnsLayout.gap = 10;
			vertBtnsLayoutGroup.layout = btnsLayout;
			buttonsLayoutGroup.addChild(vertBtnsLayoutGroup);
			
			reviewBtn = new Button();
			reviewBtn.label = "Write a Review";
			reviewBtn.nameList.add("review-btn");
			reviewBtn.addEventListener(Event.TRIGGERED, onWriteReviewTrigger);
			vertBtnsLayoutGroup.addChild(reviewBtn);
			
			managersReviewBtn = new Button();
			managersReviewBtn.label = "View Managers Review";
			managersReviewBtn.nameList.add("review-btn");
			managersReviewBtn.addEventListener(Event.TRIGGERED, onManagerReviewTrigger);
			vertBtnsLayoutGroup.addChild(managersReviewBtn);
			
			custReviewBtn = new Button();
			custReviewBtn.label = "View Customers Review";
			custReviewBtn.nameList.add("review-btn");
			custReviewBtn.addEventListener(Event.TRIGGERED, onCustomerReviewTrigger);
			vertBtnsLayoutGroup.addChild(custReviewBtn);
			
			super.initialize();
		}
		
		private function onWriteReviewTrigger(event:Event):void
		{
			this.dispatchEventWith(TRIGGER_WRITE_REVIEW, false, "writeReview");
		}
		
		private function onCustomerReviewTrigger(event:Event):void
		{
			this.dispatchEventWith(TRIGGER_CUSTOMER_REVIEW, false, "customerReview");
		}
		
		private function onManagerReviewTrigger(event:Event):void
		{
			this.dispatchEventWith(TRIGGER_MANAGERS_REVIEW, false, "managersReview");
		}
		
		override protected function draw():void
		{
			var stageWidth:int = stage.stageWidth;
			var stageHeight:int = stage.stageHeight;
			
			vertBtnsLayoutGroup.width = buttonsBackground.width = stageWidth;
			buttonsBackground.height = buttonsLayoutGroup.height;
			
			anchorLayoutGroup.width = stageWidth;
			anchorLayoutGroup.height = stageHeight - (breadcrumbComp.y + breadcrumbComp.height + buttonsLayoutGroup.height) + 20*CommonUtils.scaleValue;
			
			empTitleLblData.top = employeeName.y + employeeName.height;
			employeeImageData.bottom = starRattingComp.height + 45*CommonUtils.scaleValue;
			
			var availHeight:int = (anchorLayoutGroup.height - starRattingComp.height) - (empTitleLblData.top + employeeTitle.height) - 40;
			
			if(availHeight > 400)
			{
				employeeImage.width = employeeImage.height = availHeight - 280;
				starRatingCompData.bottom = 30;
				imageFrame.width = imageFrame.height = availHeight - 195;
				employeeImageData.bottom = starRattingComp.height + 60;
				imageFrame.y = employeeImage.y;
			}
			
			else 
			{
				employeeImage.width = employeeImage.height = availHeight;
				imageFrame.width = imageFrame.height = availHeight + 5*CommonUtils.scaleValue;
			}
			
			super.draw();
		}
		
		
		private function onMenuBtnTrigger(event:Event):void
		{
			this.dispatchEventWith(Event.COMPLETE);
		}
		
	}
}